
package com.szpx.controller.LJKController;

import com.github.pagehelper.PageInfo;
import com.szpx.entity.LJK.LJKCoach;
import com.szpx.entity.LJK.Reserve;
import com.szpx.mapper.LJKReserveMapper;
import com.szpx.service.LJKCoachService;

import com.szpx.service.LJKReserveService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单数据控制器
 */
@Controller
@RequestMapping("/ljk/coach")
public class LJKCoachController {

    @Resource
    private LJKCoachService ljkCoachService;
    @Resource
    private LJKReserveService ljkReserveService;

    @RequestMapping("/coach")
    public String coach(HttpSession session, Model model) {
        return "LJK/coach.html";
    }
    @RequestMapping("/index")
    public String list(HttpSession session, Model model) {
        return "LJK/index.html";
    }

    /**
     * 预约教练
     * @param id
     * @return
     */
    @GetMapping("/reserve")
    public ModelAndView reserve(@RequestParam Long id) {
        ModelAndView modelAndView = new ModelAndView("LJK/confirmed.html");
        modelAndView.addObject("coach", ljkCoachService.getCoachById(id));
        return modelAndView;
    }
    /**
     * 预约记录
     * @param id
     * @return
     */
    @GetMapping("reserve/html")
    public ModelAndView reserveList() {
        ModelAndView modelAndView = new ModelAndView("LJK/reserve-list.html");
        return modelAndView;
    }


    @RequestMapping("/list")
    @ResponseBody
    public Resp<List<LJKCoach>> list(@RequestParam Integer page, @RequestParam Integer limit,@ModelAttribute LJKCoach ljkCoach) {
        PageInfo<LJKCoach> coachesByPage = ljkCoachService.getCoachesByPage(page, limit,ljkCoach );
        return Resp.success(coachesByPage.getList(),coachesByPage.getTotal());
    }


    /**
     * 预约记录
     * @return
     */
    @GetMapping("reserve/list")
    @ResponseBody
    public Resp<?> reserveList(@RequestParam Integer page, @RequestParam Integer limit,@ModelAttribute Reserve reserve) {
        PageInfo<Reserve> reservePageInfo = ljkReserveService.queryAllReserve(page, limit, reserve);
        return Resp.success(reservePageInfo.getList(), reservePageInfo.getTotal());
    }

    /**
     * 预约教练
     * @param id
     * @return
     */
    @PostMapping("/insertReserve")
    @ResponseBody
    public Resp insertReserve(@RequestBody Reserve reserve) {
        reserve.setStatus(1); // 设置预约状态为待确认
        ljkReserveService.reserveCoach(reserve);
        return Resp.success();
    }
    //更新预约状态
    @PostMapping("/reserve/updateStatus")
    @ResponseBody
    public Resp updateStatus(@RequestBody Reserve reserve) {
        boolean result = ljkReserveService.updateStatus(reserve.getId(), reserve.getStatus());
        if (result) {
            return Resp.success();
        } else {
            return Resp.error("更新预约状态失败，请稍后再试。");
        }
    }


}
